 // Sabadell Analysis

cd "/Users/benmorton//Documents/Ben's documents/Research/Projects/Sabadell/Data"

import excel "Sabadell2019B", sheet("Sabadell2019") firstrow clear
sort LSOA
save Sabadell, replace

import excel "LSOA codes and scores", sheet("LSOA") firstrow clear
sort LSOA
save LSOA, replace

use "Sabadell.dta", clear
merge m:1 LSOA using "LSOA"
save Sabadel, replace

by ICU_Death _merge, sort: summ Age, detail
by ICU_Death _merge, sort: summ APACHE_II, detail

kwallis Age if ICU_Death ==1, by(_merge)
kwallis APACHE_II if ICU_Death ==1, by(_merge)

by ICU_Death, sort: tabulate _merge Gender, chi2 row
by ICU_Death, sort: tabulate _merge Hospital_Death, chi2 row
by ICU_Death, sort: tabulate _merge Med_Surg, chi2 row
by ICU_Death, sort: tabulate _merge Type, chi2 row
by ICU_Death, sort: tabulate _merge Sabadell, chi2 row

drop if _merge==1
drop if _merge==2


set more off

label variable ID "Patient ID"	
label variable Postcode	"Postcode"
label variable Age "Age"
label variable Gender "Gender"	
label variable BMI "Body Mass Index"	
label variable Type "Admission Type"	
label variable LOS "ITU LOS"	
label variable Date_admit "Admitted to ITU"	
label variable Year_admit "Year of study"	
label variable Unit_discharge "Unit discharge date"	
label variable Readmission "ITU readmission"	
label variable Readmission_spell "ITU readmission within same hospital spell"	
label variable Admissions "Number of ITU admissions"	
label variable Specialty "Admission specialty"		
label variable Med_Surg	"Medical or surgical admission"
label variable Diagnosis "Admission diagnosis"	
label variable Sabadell	"Sabadell score"
label variable Hosp_LOS	"Hospital LOS"
label variable APACHE_II "APACHE II score"	
label variable SOFA	"SOFA score"
label variable Alive "Survival status"		
label variable DOD "Date of death"	
label variable TTD	"Time to death"
label variable ICU_Death "ICU Death"
label variable Hospital_Death "Hospital Death"
label variable Direct_home "Discharged directly home"
label variable Review_date "Last medical record review"	
label variable TTRD "Time to review days"	
label variable TTRY	"Time to review in years"
label variable IMDR	"Index of Multiple Deprivation Rank"
label variable IMDRDEC "Index of Multiple Deprivation Decile"
label variable HDD	"Health and Disability Decile"
label variable HDDR "Health rank"	
label variable LSOA "LSOA Code"

// Label variables


label define gender 1 "male" 2 "female"
label values Gender gender

label define status 1 "Emergency" 2 "Elective"
label values Type status

label define readmission 1 "Yes" 2 "No"
label values Readmission Readmission_spell Direct_home readmission

label define team 1 "Cardiology" 2 "Dermatology" 3 "Endocrinology" 4 "ENT" 5 "Gastroenterology" 6 "General Medicine" 7 "General Surgery" 8 "Gynaecology" 9 "Haematology" 10 "Hepatobiliary surgery" 11 "Maxillofacial surgery" 12 "Nephrology" 13 "Neurology" 14 "Neurosurgery" 15 "Orthopaedics" 16 "Respiratory medicine" 17 "Rheumatology" 18 "Trauma" 19 "Upper gastrointestinal surgery" 20 "Urology" 21 "Vascular Surgery" 22 "Opthalmology"
label values Specialty team

label define medsurg 1 "Medical" 2 "Surgical"
label values Med_Surg medsurg

label define survival 1 "Alive" 0 "Dead"
label values Alive ICU_Death survival

label define dead 0 "Alive" 1 "Dead"
label values Hospital_Death dead

// Analysis

tabulate Readmission_spell

recode Age (min/39.999 = 1) (40/59.999 = 2)(60/79.999 = 3) (80/max = 4), gen(age_group)
label define agegroup 1 "0-40" 2 "40-60" 3 "60-80" 4 "80+"
label values age_group agegroup
tab age_group

recode BMI (min/18.50 = 1) (18.5/24.99 = 2) (25.0/29.99 = 3) (30.0/34.99 = 4) (35.0/39.99 = 5) (40/max = 6), gen(BMI_group)
label define bmi 1 "underweight" 2 "normal weight" 3 "overweight" 4 "class I obesity" 5 "class II obesity" 6 "class III obesity"
label values BMI_group bmi

// ICU death information

tab ICU_Death
by ICU_Death, sort: summ LSOA_score, detail
ranksum LSOA_score, by(ICU_Death)

// ICU survivors versus non-survivors demographics

by ICU_Death, sort: summ Age, detail

/// Wait for Ged before finalising this comparison.

// Drop ITU deaths from the analysis

drop if ICU_Death ==0

// Readmission variables

gen dischargereadmit =.
recode dischargereadmit .=2 if Readmission_spell==1
recode dischargereadmit .=1 if Readmission==1
recode dischargereadmit .=2 if Readmission==2

label values dischargereadmit readmission
label variable dischargereadmit "Readmitted to ITU after initial hospital discharge"
tabulate dischargereadmit

tabulate age_group
tabulate Gender
tabulate Type
tabulate Specialty
tabulate Med_Surg
tabulate Sabadell

summ LSOA_score, detail
swilk LSOA_score
summ IMDRDEC, detail
swilk IMDRDEC
summ Age, detail
summ APACHE_II, detail
tab Readmission 
tab Readmission_spell 
tab dischargereadmit

label define yearadmit 1 "2011" 2 "2012" 3 "2013" 4 "2014" 5 "2015" 6 "2016" 7 "2017"
label values Year_admit yearadmit
tab Year_admit

// Death Cut Offs

tab Hospital_Death

gen Thirtydaymort =.
recode Thirtydaymort .=1 if TTD <30 | TTD==30
recode Thirtydaymort .=0 if TTD >30 & TTRD>30
recode Thirtydaymort .=0 if TTRD >30
label values Thirtydaymort dead
tab Thirtydaymort

gen Ninetydaymort =.
recode Ninetydaymort .=1 if TTD <90 | TTD==90
recode Ninetydaymort .=0 if TTD >90 & TTRD>90
recode Ninetydaymort .=0 if TTRD >90
label values Ninetydaymort dead
tab Ninetydaymort

gen Sixmonth =.
recode Sixmonth .=1 if TTD <183 | TTD==183
recode Sixmonth .=0 if TTD >183 & TTRD >183
recode Sixmonth .=0 if TTRD > 183
label values Sixmonth dead
tab Sixmonth

gen Oneyearmort =.
recode Oneyearmort .=1 if TTD <365 | TTD==365
recode Oneyearmort .=0 if TTD >365 & TTRD >365
recode Oneyearmort .=0 if TTRD >365
label values Oneyearmort dead
tab Oneyearmort

gen Twoyearmort =.
recode Twoyearmort .=1 if TTD <730 | TTD==730
recode Twoyearmort .=0 if TTD>730 & TTRD>730
recode Twoyearmort .=0 if TTRD >730
label values Twoyearmort dead
tab Twoyearmort

gen Threeyearmort =.
recode Threeyearmort .=1 if TTD <1095 | TTD==1095
recode Threeyearmort .=0 if TTD >1095 & TTRD >1095
recode Threeyearmort .=0 if TTRD >1095
label values Threeyearmort dead
tab Threeyearmort

gen Fiveyearmort =.
recode Fiveyearmort .=1 if TTD <1825 | TTD ==1825
recode Fiveyearmort .=0 if TTD >1825 & TTRD >1825
recode Fiveyearmort .=0 if TTRD >1825
label values Fiveyearmort dead
tab Fiveyearmort




// Multivariate hospital mortality

logistic Hospital_Death i.Sabadell
logistic Hospital_Death ib2.Gender
logistic Hospital_Death i.Type
logistic Hospital_Death ib2.Med_Surg
logistic Hospital_Death LSOA_score
logistic Hospital_Death i.age_group
logistic Hospital_Death APACHE_II
logistic Hospital_Death ib2.Readmission_spell
logistic Hospital_Death Hosp_LOS
logistic Hospital_Death i.Year_admit

logistic Hospital_Death ib2.Gender i.Sabadell i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit


// Sabadell data

tabulate Sabadell Hospital_Death, row
tabulate Sabadell Thirtydaymort , row
tabulate Sabadell Ninetydaymort , row
tabulate Sabadell Sixmonth, row
tabulate Sabadell Oneyearmort, row
tabulate Sabadell Twoyearmort, row
tabulate Sabadell Threeyearmort, row
tabulate Sabadell Fiveyearmort, row

by age_group, sort: tabulate Sabadell Hospital_Death, row
by age_group, sort: tabulate Sabadell Thirtydaymort , row
by age_group, sort: tabulate Sabadell Ninetydaymort , row
by age_group, sort: tabulate Sabadell Sixmonth, row
by age_group, sort: tabulate Sabadell Oneyearmort, row
by age_group, sort: tabulate Sabadell Twoyearmort, row
by age_group, sort: tabulate Sabadell Threeyearmort, row
by age_group, sort: tabulate Sabadell Fiveyearmort, row

by Sabadell, sort: summ LSOA_score, detail

tabulate Sabadell Gender, chi2
tabulate Sabadell Med_Surg, chi2
tabulate Sabadell Type, chi2
tabulate Sabadell Readmission_spell, chi2 
kwallis LSOA_score, by(Sabadell)
oneway Age Sabadell, tabulate
oneway APACHE_II Sabadell, tabulate

logistic Hospital_Death i.Sabadell
lroc
logistic Thirtydaymort i.Sabadell
lroc
logistic Ninetydaymort i.Sabadell
lroc
logistic Sixmonth i.Sabadell
lroc
logistic Oneyearmort i.Sabadell
lroc
logistic Twoyearmort i.Sabadell
lroc
logistic Threeyearmort i.Sabadell
lroc
logistic Fiveyearmort i.Sabadell
lroc

